package cn.texous.demo.dmqs.config;

import cn.texous.demo.dmqs.config.objects.Config;
import com.google.common.base.Strings;
import com.typesafe.config.ConfigBeanFactory;
import com.typesafe.config.ConfigFactory;

import java.io.File;

/***/
public class ConfigurationFactory {
    /**
     * Loads configuration object by file
     *
     * @return Returns configuration objects
     */
    public static Config load() {
        if (Strings.isNullOrEmpty(System.getProperty("config"))) {
            throw new RuntimeException("Configuration file path is empty. "
                    + "Please specify the file path with using -Dconfig=[PATH]");
        }

        com.typesafe.config.Config config
                = ConfigFactory.parseFile(new File(System.getProperty("config")));

        return ConfigBeanFactory.create(config, Config.class);
    }

    /**
     * Loads configuration object by file
     *
     * @return Returns configuration objects
     */
    public static Config loadFromClasspath() {
        com.typesafe.config.Config config
                = ConfigFactory.parseString("producer {\n"
                + "  batchSize: 16384\n"
                + "  topic: \"\"\n"
                + "\n"
                + "  hosts: [\n"
                + "    \"192.168.0.250:19092\",\n"
                + "    \"192.168.0.251:19092\",\n"
                + "    \"192.168.0.252:19092\"\n"
                + "  ]\n"
                + "}");
        return ConfigBeanFactory.create(config, Config.class);
    }

}
